package com.mod.reanimator 
{
	import com.calefk.core.List;
	import com.calefk.core.EntitySimple;
	import com.calefk.core.Project;
	import com.calefk.util.helper;
	import com.calefk.util.Math2;
	import com.mod.Global;
	import flash.geom.Rectangle;
	import com.calefk.modules.math.Vector2D;
	import flash.display.MovieClip;
	
	import com.mod.enemies.*;
	
	/**
	 * ...
	 * @author Adrian
	 */
	public class Guardian extends BaseVillager
	{
		public var targetEnemy:Enemy;
		private var enemyList:List;
		
		private var attackTimer:int;
		private var attackFrecuency:int;
		private var isAttacking:Boolean = false;
		
		private var mSpeedVect:Vector2D = null;
		
		private var mAngleChange:Number = 5;
		private var mAngleCounter:Number = 0;
		private var mMaxAngleChange:Number = 90;
		
		public function Guardian(canvas:MovieClip, nameClip:String=null, px:Number=0, py:Number=0, project:Project=null,enemyManager:EnemyManager=null) 
		{
			super(canvas, nameClip, px, py, project);
			targetEnemy = null;
			enemyList = enemyManager.list;
			
			attackTimer = 1000; 
			attackFrecuency = 1000;
		}
		
		public override function update(dt:int):void
		{
			switch(currentState)
			{
				case BaseVillager.DEATH:
					mClip.gotoAndStop("dying");
					break;
				case BaseVillager.EXPLODING:
					mClip.gotoAndStop("exploding");
					break;
				case BaseVillager.MOVING:
					mClip.gotoAndStop("moving");
					break;
				case BaseVillager.REVIVING:
					mClip.gotoAndStop("reviving");
					break;
				case BaseVillager.FIGHTING:
					mClip.gotoAndStop("attacking");
					break;
			}
			
			//esta atacando. No hace nada esta vez
			if (attackTimer < attackFrecuency) {
				attackTimer += dt;
				return;
			}
			if (((targetEnemy != null) && (targetEnemy.toClean == false)) && 
			((currentState == BaseVillager.MOVING) || (currentState == BaseVillager.FIGHTING))) {
				
				if (targetEnemy.life <= 0) {
					// si murio el enemigo, busca otro
					targetEnemy = null;
					currentState = BaseVillager.MOVING;
					return;
				}
				
				if (mClip.collision_mc.hitTestObject(targetEnemy.collision)) 
				{ 
					//if (attackTimer >= attackFrecuency) {
						currentState = BaseVillager.FIGHTING;
						//trace("Guardian atacando");
						Global.playSound("snd_attack_guardian",1,1)
						targetEnemy.onReceiveDamage(-20,this);
						attackTimer = 0;
					//}	
					
				} else {
					/*var dx = enemigo.x - x;
					var dy = enemigo.y - y;
					var directionX = dx / (Math.abs(dx) + Math.abs(dy));
					var directionY = dy / (Math.abs(dx) + Math.abs(dy));
					move(speed * dt * directionX, speed * dt * directionY);
					*/
					currentState = BaseVillager.MOVING;
					
					mSpeedVect = new Vector2D(targetEnemy.x - this.x, targetEnemy.y - this.y);
					mSpeedVect.normalize();
					mSpeedVect.scale(speed * dt);
					var oldX:Number = this.x; var oldY:Number = this.y;
					move(mSpeedVect.x, mSpeedVect.y);
					//chequeo con la base central
					var baseColl:MovieClip = Global.engine.world.hearth;
					
					if (this.mClip.hitTestObject(baseColl)) {
						this.x = oldX;
						this.y = oldY;
						//buscar una nueva direccion
						for (mAngleCounter = 0; mAngleCounter <= mMaxAngleChange; mAngleCounter += mAngleChange ) {
							// decide si debe rotar positiva o negativamente
							//
							Vector2D.rotateSameVector(mSpeedVect, mAngleChange);
							move(mSpeedVect.x, mSpeedVect.y);
							if (this.mClip.hitTestObject(baseColl)) {
								// intentar de nuevo
								this.x = oldX;
								this.y = oldY;
							} else {
								break;
							}
						}
						//si acaba el for y aun no mueve se queda quieto
					}
					
				}
				
			}
			else{ 
				findTarget();
			}
			
		}
		
		public function movingToBase():void
		{
			
		}
		
		public function findTarget():void
		{
			var shortestDistance = -1;
			var count = 0;
			for each (var enemy in enemyList.data)
			{
				if (enemy.life > 0){
					var distance:Number = Math2.Pythagoras(x, y, enemy.x, enemy.y);
					if ((shortestDistance == -1) || (distance < shortestDistance)) {
						shortestDistance = distance;
						targetEnemy = enemy;
					}
				}
				count++;
			}
			
		}
		
	}

}